<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1100" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <!-- <el-col :span="8">
                    <el-form-item label="BOM单编号" prop="bom_code">
                        <el-input v-model="form.bom_code" readonly placeholder="自动生成" />
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="BOM分类" prop="cate_id">
                        <el-select v-model="form.cate_id" placeholder="请选择分类" style="width:100%" clearable filterable>
                            <el-option v-for="item in cateList" :key="item.id" :label="item.cate_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="产品型号" prop="goods_id">
                        <el-input v-model="goods_model" placeholder="请选择产品" @click="checkGoods(1)" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="速比">
                        <el-input v-model="speed" placeholder="速比" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="法兰">
                        <el-input v-model="flange" placeholder="法兰" readonly></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="颜色">
                        <el-input v-model="color" placeholder="颜色" readonly></el-input>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-col :span="24">
                <el-form-item label="设置物料" prop="bom_child">
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="checkMater(2)">选择物料</el-button>
                    <sc-form-table ref="table" v-model="form.bom_child" :addTemplate="addTemplate" placeholder="暂无数据" drag-sort height="250" hideAdd>
                        <el-table-column prop="goods_model" label="物料型号" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.goods_model" placeholder="物料型号" disabled></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="速比" align="center"></el-table-column>
                        <el-table-column label="法兰" align="center"></el-table-column>
                        <!-- <el-table-column label="颜色" align="center" prop="color_name"></el-table-column> -->
                        <el-table-column prop="number" label="数量" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" placeholder="数量"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsupdate" @closed="dialog.goods=false" :query="{get_product:1}"></select-goods>
    <select-mater v-if="dialog.mater" ref="materDialog" @success="materupdate" @closed="dialog.mater=false" :query="{get_material:1}"></select-mater>
</template>

<script>
import selectGoods from '../goods/info/selectGoods.vue';
import selectMater from '../goods/info/selectGoods.vue';
export default {
    components: { selectGoods,selectMater },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增BOM',
				copy: '新增BOM',
				edit: '编辑BOM',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                goods_id: "",
                cate_id: "",
                bom_child: [],
			},
			//验证规则
			rules: {
				goods_id: [{ required: true, message: '请选择产品' }],
				cate_id: [{ required: true, message: '请选择bom分类' }],
			},
            goods_model: "",
            speed: "",
            flange: "",
            color: "",
            addTemplate:{},
            dialog:{
                goods: false,
                mater: false,
            },
            cateList: []
		}
	},
	mounted() {
        this.getCate()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.bom.edit.post(this.form);
					} else {
						res = await this.$API.goods.bom.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.goods.bom.detail.get({id:data.id});
            this.form.goods_id = res.data.goods_id
            this.goods_model = res.data.goods && res.data.goods.goods_model
            this.speed = res.data.goods && res.data.goods.speedRatio && res.data.goods.speedRatio.name
            this.flange = res.data.goods && res.data.goods.flange && res.data.goods.flange.name
            this.color = res.data.goods && res.data.goods.color && res.data.goods.color.name
            this.form.cate_id = res.data.cate_id
            let array = []
            res.data.child_all.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    number:item.number,
                    goods_model:item.goods && item.goods.goods_model,
                    color_name:item.goods && item.goods.color && item.goods.color.name,
                })
            })
            this.form.bom_child = array
		},
        onFocus(event){
            event.target.select();
        },
        // 选择产品
        checkGoods(e){
            this.dialog.goods = true
            this.$nextTick(() => {
                this.$refs.goodsDialog.open('add').setData(e)
            })
        },
        goodsupdate(data){
            this.form.goods_id = data[0].id
            this.goods_model = data[0].goods_model
            this.speed = data[0].speed_ratio && data[0].speed_ratio.name
            this.flange = data[0].flange && data[0].flange.name
            this.color = data[0].color && data[0].color.name
        },
        checkMater(e){
            this.dialog.mater = true
            this.$nextTick(() => {
                this.$refs.materDialog.open('add').setData(e)
            })
        },
        materupdate(data){
            let array = []
            data.map((item)=>{
                array.push({
                    goods_id:item.id,
                    goods_model:item.goods_model,
                    color_name:item.color && item.color.name,
                })
            })
            let existingIds = this.form.bom_child.map(item => item.goods_id);
            for(let i = array.length - 1; i >= 0; i--){
                if(existingIds.includes(array[i].goods_id)){
                    array.splice(i, 1);
                }
            }
            this.form.bom_child = [...this.form.bom_child,...array]
        },
        // 获取分类
        async getCate(){
            var res = await this.$API.goods.bomCate.all.get();
            this.cateList = res.data
        },
        async setCopy(data) {
			var res = await this.$API.goods.bom.detail.get({id:data.id});
            this.form.goods_id = res.data.goods_id
            this.goods_model = res.data.goods && res.data.goods.goods_model
            this.speed = res.data.goods && res.data.goods.speedRatio && res.data.goods.speedRatio.name
            this.flange = res.data.goods && res.data.goods.flange && res.data.goods.flange.name
            this.color = res.data.goods && res.data.goods.color && res.data.goods.color.name
            this.form.cate_id = res.data.cate_id
            let array = []
            res.data.child_all.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    number:item.number,
                    goods_model:item.goods && item.goods.goods_model,
                    color_name:item.goods && item.goods.color && item.goods.color.name,
                })
            })
            this.form.bom_child = array
		},
	}
}
</script>

<style></style>
